package com.chat.service;

import com.chat.common.Message;
import com.chat.common.MessageType;
import com.chat.common.User;
import com.chat.view.FriendListData;
import com.chat.view.MainWindowController;
import com.chat.view.MainWindowView;
import javafx.application.Platform;
import javafx.scene.control.Alert;

import java.util.Iterator;

import static com.chat.view.MainWindowController.friend;
import static com.chat.view.MainWindowController.chat;
/*
  该类用于删除好友
 */
public class DelFriend {
      public void delfriend(String id,String delid){//当前用户的Id和要删除的用户的id
            Message msg=new Message();
            User user=new User();
            msg.setMesType(MessageType.MEESAGE_DEL_FRIEND);
            user.setUsername(id);
            user.setDelid(delid);
            msg.setData(user);
            ClientConnectServerThread clientConnectServerThread=ManageClientConnectServerThread.getClientConnectServerThread(id);
            clientConnectServerThread.send(msg);
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText("恭喜");
            alert.setContentText("您已成功删除该好友！");
            alert.showAndWait();
            Platform.runLater(()-> {
                  ((javafx.scene.control.ListView) MainWindowView.$("chatlist")).getItems().clear();
            });
            chat.clear();
            int line=0;
            Iterator<FriendListData>iter=friend.iterator();
            while(iter.hasNext()){
                  FriendListData friendListData=(FriendListData) iter.next();
                  System.out.println(friendListData.getId());
                  if(friendListData.getId().equals(delid)){
                        friend.remove(line);
                        break;
                  }
                  line++;
            }
//            Platform.runLater(new Runnable() {
//                  @Override
//                  public void run() {
//                        ((javafx.scene.control.ListView) MainWindowView.$("listview")).getItems().clear();
//                  }
//            });
//            friend.clear();
//            new GetFriendList(friend);
//            GetGroupList ggl=new GetGroupList();
//            ggl.getgrouplist();
      }
      public void setfriend(){//接受端实时删除好友列表中的好友
            Platform.runLater(()-> {
                  ((javafx.scene.control.ListView) MainWindowView.$("chatlist")).getItems().clear();
            });
            chat.clear();
//            Platform.runLater(new Runnable() {
//                  @Override
//                  public void run() {
//                        ((javafx.scene.control.ListView) MainWindowView.$("listview")).getItems().clear();
//                  }
//            });
//
//            friend.clear();
//            new GetFriendList(friend);
//            GetGroupList ggl=new GetGroupList();
//            ggl.getgrouplist();
      }
}
